<template>
  <ContentWrap>
    <div class="routeHeader mb-5px">
    <el-button type="primary" @click="deleteData" v-hasPermi="['config:manufacturingPartsListTask:delete']">批量删除</el-button>
    <el-button type="primary"  @click="save" v-hasPermi="['config:manufacturingPartsListTask:save']">保存</el-button>
    <el-button type="primary"  @click="submitData" v-hasPermi="['config:manufacturingPartsListTask:submit']">提交</el-button>
    <el-button  @click="closeData">关闭</el-button>
  </div>
  </ContentWrap>
  
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          disabled
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          disabled
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="图纸图号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图纸图号"
          clearable
          disabled
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品规格" prop="productStandard">
        <el-input
          v-model="queryParams.productStandard"
          placeholder="请输入产品规格"
          clearable
          disabled
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          disabled
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list" border @selection-change="handleSelectionChange" :header-cell-style="headFirst" :height="tableHeight">
      <el-table-column type="selection" width="55" :selectable="isSelectable" fixed="left"/>
      <el-table-column align="left" label="编号">
        <el-table-column align="left" prop="code1" width="80">
          <template #default="scope">
            <el-input v-nextInput="'code1'" name="code1" v-model="scope.row.code1" :disabled="scope.row.isCanDisable" />
          </template>
        </el-table-column>
        <el-table-column align="left" prop="code2" width="80">
          <template #default="scope">
            <el-input v-nextInput="'code2'" name="code2" v-model="scope.row.code2" :disabled="scope.row.isCanDisable" />
          </template>
        </el-table-column>
        <el-table-column align="left" prop="code3" width="80">
          <template #default="scope">
            <el-input v-nextInput="'code3'" name="code3" v-model="scope.row.code3" :disabled="scope.row.isCanDisable" />
          </template>
        </el-table-column>
        <el-table-column align="left" prop="code4" width="80">
          <template #default="scope">
            <el-input v-nextInput="'code4'" name="code4" v-model="scope.row.code4" :disabled="scope.row.isCanDisable" />
          </template>
        </el-table-column>
        <el-table-column align="left" prop="code5" width="80">
          <template #default="scope">
            <el-input v-nextInput="'code5'" name="code5" v-model="scope.row.code5" :disabled="scope.row.isCanDisable" />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="部件编号" align="left" prop="partNumber" width="180">
        <template #default="scope">
          <el-input v-nextInput="'partNumber'" name="partNumber" v-model="scope.row.partNumber" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="部件名称" align="left" prop="partName" width="180">
        <template #default="scope">
          <el-input v-nextInput="'partName'" name="partName" v-model="scope.row.partName" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="图纸图号" align="left" prop="drawingNumber" width="180">
        <template #default="scope">
          <el-input v-nextInput="'drawingNumber'" name="drawingNumber" v-model="scope.row.drawingNumber" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="部件规格" align="left" prop="partSpecification" width="180">
        <template #default="scope">
          <el-input v-nextInput="'partSpecification'" name="partSpecification" v-model="scope.row.partSpecification" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="材质" align="left" prop="material" width="180">
        <template #default="scope">
          <el-input v-nextInput="'material'" name="material" v-model="scope.row.material" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="规格" align="left" prop="specification" width="180">
        <template #default="scope">
          <el-input v-nextInput="'specification'" name="specification" v-model="scope.row.specification" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="净尺寸(长)" align="left" prop="netSizeLength" width="180">
        <template #default="scope">
          <el-input
            v-nextInput="'netSizeLength'" 
            name="netSizeLength"
            v-model="scope.row.netSizeLength"
            type="number"
            :min="0"
            :disabled="scope.row.isCanDisable"
          />
        </template>
      </el-table-column>
      <el-table-column label="净尺寸(宽)" align="left" prop="netSizeWidth" width="180">
        <template #default="scope">
          <el-input
            v-nextInput="'netSizeWidth'" 
            name="netSizeWidth"
            v-model="scope.row.netSizeWidth"
            type="number"
            :min="0"
            :disabled="scope.row.isCanDisable"
          />
        </template>
      </el-table-column>
      <el-table-column label="下料尺寸(长)" align="left" prop="cuttingSizeLength" width="180">
        <template #default="scope">
          <el-input
            v-nextInput="'cuttingSizeLength'" 
            name="cuttingSizeLength"
            v-model="scope.row.cuttingSizeLength"
            type="number"
            :min="0"
            :disabled="scope.row.xlDisable"
            @blur="changeData(scope.$index)"
          />
        </template>
      </el-table-column>
      <el-table-column label="下料尺寸(宽)" align="left" prop="cuttingSizeWidth" width="180">
        <template #default="scope">
          <el-input
            v-nextInput="'cuttingSizeWidth'" 
            name="cuttingSizeWidth"
            v-model="scope.row.cuttingSizeWidth"
            type="number"
            :min="0"
            :disabled="scope.row.xlDisable"
            @blur="changeData(scope.$index)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="基本用量"
        align="left"
        prop="basicDosage"
        width="180"
        type="number"
        :min="0"
      >
        <template #default="scope">
          <el-input v-nextInput="'basicDosage'" name="basicDosage" v-model="scope.row.basicDosage" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="单位" align="left" prop="partUnit" width="180">
        <template #default="scope">
          <el-input v-nextInput="'basicDosage'" name="basicDosage" v-model="scope.row.partUnit" :disabled="scope.row.isCanDisable" />
        </template>
      </el-table-column>
      <el-table-column label="内作" align="left" prop="dictInternalWork" width="180">
        <template #default="scope">
          <input
            type="checkbox"
            v-model="scope.row.dictInternalWork"
            :disabled="scope.row.isCanDisable"
            @change="handleDictInternalWorkChange(scope.$index)"
            class="custom-checkbox"
          />
        </template>
      </el-table-column>
      <el-table-column label="外协" align="left" prop="dictOutsourcing" width="180">
        <template #default="scope">
          <input
            type="checkbox"
            :disabled="scope.row.isCanDisable"
            class="custom-checkbox"
            v-model="scope.row.dictOutsourcing"
            @change="handleDictOutsourcingChange(scope.$index)"
          />
        </template>
      </el-table-column>
      <el-table-column label="工序名称" align="left" prop="dictProcessName" width="180">
        <template #default="scope">
          <el-select
            :disabled="scope.row.isCanDisable"
            v-model="scope.row.dictProcessName"
            placeholder="请选择工序名称"
            required
            @change="handleDictProcessNameChange(scope.$index)"
          >
            <el-option
              v-for="(dict, index) in dictProcessNameOptions"
              :key="index"
              :label="dict.label"
              :value="dict.dictProcessName"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="下料工序" align="left" prop="dictCuttingProcess" width="180">
        <template #default="scope">
          <el-select
            v-model="scope.row.dictCuttingProcess"
            placeholder="请选择下料工序"
            required
            :disabled="scope.row.disabled"
          >
            <el-option
              v-for="(dict, index) in getIntDictOptions(
                DICT_TYPE.CONFIG_MANUFACTURING_DETAILS_CUTTING_PROCESS
              )"
              :key="index"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="下料类型" align="left" prop="dictCuttingType" width="180">
        <template #default="scope">
          <el-select
            v-model="scope.row.dictCuttingType"
            placeholder="请选择下料类型"
            required
            :disabled="scope.row.disabled"
          >
            <el-option
              v-for="(dict, index) in getIntDictOptions(
                DICT_TYPE.CONFIG_MANUFACTURING_DETAILS_CUTTING_TYPE
              )"
              :key="index"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="是否机加" align="left" prop="dictIsMachining" width="180">
        <template #default="scope">
          <input
            type="checkbox"
            v-model="scope.row.dictIsMachining"
            class="custom-checkbox"
            :disabled="scope.row.isCanDisable"
          />
        </template>
      </el-table-column>
      <el-table-column label="机加工序" align="left" prop="dictMachiningProcess" width="180">
        <template #default="scope">
          <el-select
            v-model="scope.row.dictMachiningProcess"
            placeholder="请选择机加工序"
            :disabled="scope.row.isCanDisable"
            required
          >
            <el-option
              v-for="(dict, index) in getIntDictOptions(DICT_TYPE.MACHINING_PROCESS)"
              :key="index"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" prop="dictStatus" width="180">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.CONFIG_MANUFACTURING_DETAILS_STATUS"
            :value="scope.row.dictStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="left" prop="userNickName" width="180">
        <template #default="scope">
          <span>{{ scope.row.userNickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="left" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ timestampToDateTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="400" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="addRows(scope.$index)" v-if="scope.row.dictStatus != 2">
            新增行
          </el-button>
          <el-button
            link
            type="primary"
            @click="upDataRow(scope.$index)"
            v-if="scope.row.dictStatus != 2"
          >
            修改行
          </el-button>
          <el-button
            link
            type="primary"
            @click="cancle(scope.$index)"
            v-if="scope.row.dictStatus != 2"
          >
            取消
          </el-button>
          <el-button
            link
            type="danger"
            @click="reCutting(scope.$index)"
            v-if="scope.row.dictStatus != 2"
          >
            重下料
          </el-button>
          <el-button
            link
            type="danger"
            @click="deleteRow(scope.$index)"
            v-if="scope.row.dictStatus != 2"
          >
            删除
          </el-button>
          <el-button
            link
            type="primary"
            @click="restore(scope.$index)"
            v-if="scope.row.dictStatus == 2"
          >
            恢复
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ManufacturingPartsListTaskForm ref="formRef" @success="getList" />
  <!-- 上传文件 -->
  <CusUploadFile ref="cusUploadFile" @refresh="getList" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" />
</template>

<script setup lang="ts" name="ManufacturingPartsListTaskChange">
  import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  import * as ManufacturingPartsListTaskApi from '@/api/config/manufacturingPartsListTask'
  import ManufacturingPartsListTaskForm from './ManufacturingPartsListTaskForm.vue'
  import { useTagsViewStore } from '@/store/modules/tagsView'
  const message = useMessage() // 消息弹窗
  const route = useRoute()
  const tagsViewStore = useTagsViewStore()
  const routes = useRouter() // 路由
  const { push } = useRouter()
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const list: any = ref([]) // 列表的数据
  const listBackupData: any = ref([])
  const queryParams: any = reactive({
    pageNo: 1,
    pageSize: 50,
    taskNumber: null,
    completionOrChangeNumber: null,
    productNumber: null,
    productName: null,
    drawingNumber: null,
    specification: null,
    partName: null,
    taskType: null,
    status: null,
    version: null,
    activeData: [],
    stopData: [],
    userNickName: null,
    createTime: []
  })
  const dictProcessNameOptions = ref([])
  const headFirst = ({ rowIndex }) => {
    if (rowIndex == 1) {
      return { display: 'none' }
    }
  }
  //获取工序名称下拉列表
  const getdictProcessNameOptions = async() => {
    const data = await ManufacturingPartsListTaskApi.getCraftProductProcessPage()
    dictProcessNameOptions.value = data
  }
  /**校验 */
  const changeData = (index: number) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        if (list.value[index].id) {
          for (let j = 0; j < listBackupData.value.length; j++) {
            if (listBackupData.value[j].id == list.value[index].id) {
              if (list.value[index].isCanDisable == false && list.value[index].xlDisable == false) {
                if (list.value[index].cuttingSizeLength > listBackupData.value[j].cuttingSizeLength) {
                  message.warning('下料尺寸不能大于变更前尺寸')
                  list.value[index].cuttingSizeLength = listBackupData.value[j].cuttingSizeLength
                }
                console.log(
                  list.value[index].cuttingSizeWidth,
                  listBackupData.value[j].cuttingSizeWidth
                )
                if (list.value[index].cuttingSizeWidth > listBackupData.value[j].cuttingSizeWidth) {
                  message.warning('下料尺寸不能大于变更前尺寸')
                  list.value[index].cuttingSizeWidth = listBackupData.value[j].cuttingSizeWidth
                }
              } else {
                if (list.value[index].cuttingSizeLength < listBackupData.value[j].cuttingSizeLength) {
                  message.warning('下料尺寸不能小于变更前尺寸')
                  list.value[index].cuttingSizeLength = listBackupData.value[j].cuttingSizeLength
                }
                if (list.value[index].cuttingSizeWidth < listBackupData.value[j].cuttingSizeWidth) {
                  message.warning('下料尺寸不能小于变更前尺寸')
                  list.value[index].cuttingSizeWidth = listBackupData.value[j].cuttingSizeWidth
                }
              }
            }
          }
        }
      }
    }
  }
  const multipleSelection = ref([])
  const handleSelectionChange = (val) => {
    multipleSelection.value = val
    console.log(multipleSelection.value)
  }
  const isSelectable = (row) => {
    // 根据row的特定条件判断是否可勾选
    if (row.isSelectDisable) {
      return true // 可勾选
    } else {
      return false // 不可勾选
    }
  }
  /**批量删除 */
  const deleteData = async () => {
    if (multipleSelection.value.length) {
      let arr = []
      for (let i = 0; i < multipleSelection.value.length; i++) {
        arr.push(multipleSelection.value[i]['id'])
      }
      const data = await ManufacturingPartsListTaskApi.deleteSelect(arr)
      if (data == true) {
        message.success('删除成功')
        tagsViewStore.delView(routes.currentRoute.value)
        push('/config/config/materiel/manufacturingPartsListTask')
        loading.value = false
      } else {
        message.warning('删除失败')
        loading.value = false
      }
    } else {
      message.warning('请勾选要删除的数据')
    }
  }
  //时间戳转换
  const timestampToDateTime = (timestamp) => {
    if (timestamp) {
      const date = new Date(timestamp)
      const year = date.getFullYear()
      const month = ('0' + (date.getMonth() + 1)).slice(-2)
      const day = ('0' + date.getDate()).slice(-2)
      const hours = ('0' + date.getHours()).slice(-2)
      const minutes = ('0' + date.getMinutes()).slice(-2)
      const seconds = ('0' + date.getSeconds()).slice(-2)
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    } else {
      return ''
    }
  }
  const queryFormRef = ref() // 搜索的表单
  //新增行
  // const addRow = () => {
  //   let json = {
  //     code1: '',
  //     code2: '',
  //     code3: '',
  //     code4: '',
  //     code5: '',
  //     code: '', //编号
  //     partNumber: '', //部件编号
  //     partName: '', //部件名称
  //     drawingNumber: '', //图纸编号
  //     partSpecification: '', //部件规格
  //     material: '', //材质
  //     specification: '', //规格
  //     netSizeLength: '', //净尺寸长度
  //     netSizeWidth: '', //净尺寸宽度
  //     cuttingSizeLength: '', //下料尺寸长度
  //     cuttingSizeWidth: '', //下料尺寸宽度
  //     basicDosage: '', //基本用量
  //     partUnit:'',
  //     dictInternalWork: '', //内做
  //     dictOutsourcing: '', //外协
  //     dictProcessName: '', //工序名称
  //     dictCuttingProcess: '', //下料工序
  //     dictCuttingType: '', //下料类型
  //     dictIsMachining: '', //是否机加
  //     dictMachiningProcess: '', //机加工序
  //     dictStatus: '', //状态
  //     userNickName: '', //创建人
  //     createTime: '', //创建时间
  //     disabled: false,
  //     isCanDisable: false,
  //     xlDisable: false,
  //     isSelectDisable: true,
  //     id: null, //详情id
  //     taskId: route.query.id //制造件清单任务单id
  //   }
  //   list.value.push(json)
  // }
  //新增行
  const addRows = (index) => {
    let json = {
      code1: '',
      code2: '',
      code3: '',
      code4: '',
      code5: '',
      code: '', //编号
      partNumber: '', //部件编号
      partName: '', //部件名称
      drawingNumber: '', //图纸编号
      partSpecification: '', //部件规格
      material: '', //材质
      specification: '', //规格
      netSizeLength: '', //净尺寸长度
      netSizeWidth: '', //净尺寸宽度
      cuttingSizeLength: '', //下料尺寸长度
      cuttingSizeWidth: '', //下料尺寸宽度
      basicDosage: '', //基本用量
      dictInternalWork: '', //内做
      dictOutsourcing: '', //外协
      dictProcessName: '', //工序名称
      dictCuttingProcess: '', //下料工序
      dictCuttingType: '', //下料类型
      dictIsMachining: '', //是否机加
      dictMachiningProcess: '', //机加工序
      dictStatus: '', //状态
      userNickName: '', //创建人
      createTime: '', //创建时间
      partUnit:'',
      disabled: false, //
      id: null, //详情id
      taskId: route.query.id //制造件清单任务单id
    }
    list.value.splice(index+1, 0, json);
  }
  //修改行
  const upDataRow = (index: number) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        if (list.value[i].dictProcessName != 10) {
          list.value[i].disabled = true
        } else {
          list.value[i].disabled = false
        }
        list.value[i].isCanDisable = false
        list.value[i].xlDisable = false
        list.value[i].isSelectDisable = false
      }
    }
  }
  //删除行
  const deleteRow = (index: number) => {
    list.value.splice(index, 1)
  }
  //取消
  const cancle = (index: number) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        list.value[i].disabled = true
        list.value[i].isCanDisable = true
        list.value[i].xlDisable = true
        list.value[i].isSelectDisable = false
        list.value[i].dictStatus = 2
      }
    }
  }
  //恢复
  const restore = (index: number) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        list.value[i].disabled = true
        list.value[i].isCanDisable = true
        list.value[i].isSelectDisable = true
        list.value[i].dictStatus = null
      }
    }
  }

  //重下料
  const reCutting = (index: number) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        list.value[i].xlDisable = false
        list.value[i].isCanDisable = true
        list.value[i].disabled = true
      }
    }
  }

  //监听内作变换
  const handleDictInternalWorkChange = (index) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        list.value[i].dictOutsourcing = false
      }
    }
  }
  //监听外协变化
  const handleDictOutsourcingChange = (index) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        list.value[i].dictInternalWork = false
      }
    }
  }
  //监听工序名称的变化
  const handleDictProcessNameChange = (index) => {
    for (let i = 0; i < list.value.length; i++) {
      if (i == index) {
        if (list.value[i].dictProcessName != 10) {
          list.value[i].dictCuttingProcess = null
          list.value[i].dictCuttingType = null
          list.value[i].disabled = true
        } else {
          list.value[i].disabled = false
        }
      }
    }
  }
  const isCheck = () => {
    list.value
    for (let i = 0; i < list.value.length; i++) {
      console.log(list.value[i])
      if (
        (list.value[i].code1 != '' &&
          list.value[i].code2 == '' &&
          list.value[i].code3 == '' &&
          list.value[i].code4 == '' &&
          list.value[i].code5 == '') ||
        (list.value[i].code2 != '' &&
          list.value[i].code1 == '' &&
          list.value[i].code3 == '' &&
          list.value[i].code4 == '' &&
          list.value[i].code5 == '') ||
        (list.value[i].code3 != '' &&
          list.value[i].code1 == '' &&
          list.value[i].code2 == '' &&
          list.value[i].code4 == '' &&
          list.value[i].code5 == '') ||
        (list.value[i].code4 != '' &&
          list.value[i].code1 == '' &&
          list.value[i].code2 == '' &&
          list.value[i].code3 == '' &&
          list.value[i].code5 == '') ||
        (list.value[i].code5 != '' &&
          list.value[i].code1 == '' &&
          list.value[i].code2 == '' &&
          list.value[i].code3 == '' &&
          list.value[i].code4 == '')
      ) {
        list.value[i].code =
          list.value[i].code1 ||
          list.value[i].code2 ||
          list.value[i].code3 ||
          list.value[i].code4 ||
          list.value[i].code5
      } else {
        message.warning('请填写编号，且只能填一个')
        return false
      }
    }
    return true
  }
  //保存
  const save = async () => {
    let iscan = isCheck()
    if (iscan) {
      let reqData = JSON.parse(JSON.stringify(list.value))

      for (let i = 0; i < reqData.length; i++) {
        if (reqData[i].dictInternalWork == false) {
          reqData[i].dictInternalWork = 20
        } else {
          reqData[i].dictInternalWork = 10
        }
        if (reqData[i].dictOutsourcing == false) {
          reqData[i].dictOutsourcing = 20
        } else {
          reqData[i].dictOutsourcing = 10
        }
        if (reqData[i].dictIsMachining == false) {
          reqData[i].dictIsMachining = 20
        } else {
          reqData[i].dictIsMachining = 10
        }
      }
      const data = await ManufacturingPartsListTaskApi.changeSavedata(reqData)
      if (data == true) {
        message.success('保存成功')
        tagsViewStore.delView(routes.currentRoute.value)
        push('/config/config/materiel/manufacturingPartsListTask')
      } else {
        message.warning('保存失败')
      }
    }
  }
  //提交
  const submitData = async () => {
    let iscan = isCheck()
    if (iscan) {
      let reqData = JSON.parse(JSON.stringify(list.value))

      for (let i = 0; i < reqData.length; i++) {
        if (reqData[i].dictInternalWork == false) {
          reqData[i].dictInternalWork = 20
        } else {
          reqData[i].dictInternalWork = 10
        }
        if (reqData[i].dictOutsourcing == false) {
          reqData[i].dictOutsourcing = 20
        } else {
          reqData[i].dictOutsourcing = 10
        }
        if (reqData[i].dictIsMachining == false) {
          reqData[i].dictIsMachining = 20
        } else {
          reqData[i].dictIsMachining = 10
        }
      }
      const data = await ManufacturingPartsListTaskApi.changeSubmitdata(reqData)
      if (data == true) {
        message.success('提交成功')
        tagsViewStore.delView(routes.currentRoute.value)
        push('/config/config/materiel/manufacturingPartsListTask')
      } else {
        message.warning('提交失败')
      }
    }
  }
  //关闭
  const closeData = () => {
    tagsViewStore.delView(routes.currentRoute.value)
    push('/config/config/materiel/manufacturingPartsListTask')
  }
  /** 查询列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await ManufacturingPartsListTaskApi.getManufacturingPartsListTaskPage(queryParams)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }
  /**导入显示 */
  const cusUploadFile = ref()
  /** 导出按钮操作 */

  /**获取详情数据 */
  const getDetail = async (id) => {
    const data = await ManufacturingPartsListTaskApi.getDetail(id)

    for (let i = 0; i < data.length; i++) {
      if ((data[i].code.match(/\-/g) || []).length == 0) {
        data[i].code1 = data[i].code
        data[i].code2 = ''
        data[i].code3 = ''
        data[i].code4 = ''
        data[i].code5 = ''
      } else if ((data[i].code.match(/\-/g) || []).length == 1) {
        data[i].code2 = data[i].code
        data[i].code1 = ''
        data[i].code3 = ''
        data[i].code4 = ''
        data[i].code5 = ''
      } else if ((data[i].code.match(/\-/g) || []).length == 2) {
        data[i].code3 = data[i].code
        data[i].code2 = ''
        data[i].code1 = ''
        data[i].code4 = ''
        data[i].code5 = ''
      } else if ((data[i].code.match(/\-/g) || []).length == 3) {
        data[i].code4 = data[i].code
        data[i].code2 = ''
        data[i].code3 = ''
        data[i].code1 = ''
        data[i].code5 = ''
      } else if ((data[i].code.match(/\-/g) || []).length == 4) {
        data[i].code5 = data[i].code
        data[i].code2 = ''
        data[i].code3 = ''
        data[i].code4 = ''
        data[i].code1 = ''
      } else {
        data[i].code1 = ''
        data[i].code2 = ''
        data[i].code3 = ''
        data[i].code4 = ''
        data[i].code5 = ''
      }
      data[i].isCanDisable = true
      data[i].disabled = true
      data[i].xlDisable = true
      data[i].isSelectDisable = true
      data[i].dictCuttingProcess = Number(data[i].dictCuttingProcess)
      data[i].dictCuttingType = Number(data[i].dictCuttingType)
      data[i].dictMachiningProcess = Number(data[i].dictMachiningProcess)
      if (data[i].dictInternalWork == 10) {
        data[i].dictInternalWork = true
      } else {
        data[i].dictInternalWork = false
      }
      if (data[i].dictOutsourcing == 10) {
        data[i].dictOutsourcing = true
      } else {
        data[i].dictOutsourcing = false
      }
      if (data[i].dictIsMachining == 10) {
        data[i].dictIsMachining = true
      } else {
        data[i].dictIsMachining = false
      }
    }
    for(let i = 0;i<data.length;i++) {
      if(data[i].dictCuttingProcess == "") {
        data[i].dictCuttingProcess = null
      }
      if(data[i].dictCuttingType == "") {
        data[i].dictCuttingType = null
      }
      if(data[i].dictMachiningProcess == "") {
        data[i].dictMachiningProcess = null
      }
    }
    list.value = data
    
    listBackupData.value = JSON.parse(JSON.stringify(list.value))
  }
  /** 初始化 **/
  const tableHeight = ref(0)
  onMounted(() => {
    queryParams.productNumber = route.query.productNumber
    queryParams.productName = route.query.productName
    queryParams.drawingNumber = route.query.drawingNumber
    queryParams.productStandard = route.query.productStandard
    queryParams.version = route.query.version
    const queryParamsHeight = queryFormRef.value.$el.clientHeight + 20
    tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 130
    getDetail(route.query.id)
    getdictProcessNameOptions()
  })
</script>
<style lang="scss" scoped>
  input[type='checkbox'] {
    /* 隐藏默认的复选框 */
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    /* 添加自定义样式 */
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid #888;
    position: relative;
  }

  input[type='checkbox']::before {
    content: '';
    width: 10px;
    height: 10px;
    background-color: #888; /* 实心点的颜色 */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    opacity: 0; /* 默认隐藏实心点 */
  }

  input[type='checkbox']:checked::before {
    opacity: 1; /* 显示实心点 */
  }
</style>
